<template>
  <div style="text-align: center; margin-top:200px;">
    <el-row>
      <el-col :span="12" :offset="6">
        <el-form :model="pwdInfo" status-icon :rules="rules" ref="pwdInfo" label-width="100px" class="demo-ruleForm">
          <el-form-item label="原密码" prop="oldPwd">
            <el-input type="password" v-model="pwdInfo.oldPwd" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newPwd">
            <el-input type="password" v-model="pwdInfo.newPwd" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPwd">
            <el-input type="password" v-model="pwdInfo.checkPwd" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="chgPwd">提交</el-button>
            <el-button @click="resetForm('pwdInfo')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

  </div>
</template>

<script>
  export default{
    data(){
      return {
        pwdInfo: {
          oldPwd: "",
          newPwd: "",
          checkPwd:""
        },
        rules: {
          oldPwd: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
          ],
          newPwd: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
          ],
          checkPwd: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
          ]
        }
      }
    },
    mounted() {
      //从本地取出user对象
        this.user = JSON.parse(sessionStorage.getItem("user"))
    },
    methods: {
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      chgPwd: function(){
        //使用element ui自带的验证规则，如果未通过，返回不执行
        let flag = true;
        this.$refs['pwdInfo'].validate((valid) => {
          if (!valid) {
            flag = false;
          }
        });
        if(!flag){
          return;
        }

        if(this.pwdInfo.newPwd != this.pwdInfo.checkPwd){
          this.$message.error("两次输入的密码不一致")
          return
        }
        if(this.pwdInfo.oldPwd != this.user.password){
          console.log(this.user.password)
          this.$message.error("原密码错误")
          return
        }
        this.$axios.put("sysuser/password", {
          "username":this.user.username,
          "password":this.pwdInfo.newPwd
        }).then(res => {
          //if(res.data.status == "SUCCESS"){
            this.$message.success("修改成功，您需要重新登录")

            this.$router.push("/logout")
          //}else{
            //this.$message.error("修改失败")
          //}
        })
      }
    }
  }
</script>

<style scoped>

</style>
